package com.store.service.impl;

import com.store.entity.Product;
import com.store.mapper.ProductMapper;
import com.store.service.ProductService;
import com.store.service.ex.ProductNotFoundException;
import com.store.service.ex.UpdateException;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@DubboService(interfaceClass = ProductService.class , version = "1.0.0" ,timeout = 5000)
public class ProductServiceImpl implements ProductService{

    @Resource
    private ProductMapper productMapper;

    /**
     * 查询商品信息
     * @param id 商品id
     * @return
     */
    @Override
    public Product getProductById(Integer id) {
        Product exists = productMapper.selectProduct(id);
        if(exists == null){
            throw new ProductNotFoundException("商品不存在！");
        }else {
            //过滤数据
            exists.setStatus(null);
            exists.setCreatedUser(null);
            exists.setCreatedTime(null);
            exists.setModifiedUser(null);
            exists.setModifiedTime(null);
        }
        return exists;
    }

    /**
     * 更新商品数量（根据id）
     * @param id 商品id
     * @param username 执行者
     */
    @Override
    public void updateProductById(Integer id ,String username) {
        Product exists = productMapper.selectProduct(id);
        if(exists == null){
            throw new ProductNotFoundException("商品不存在！");
        }else {
            //封装数据
            Product product = new Product();
            product.setNum(1);
            product.setModifiedUser(username);
            product.setModifiedTime(new Date());
            int rows = productMapper.updateProductById(product);
            if(rows != 1){
                throw new UpdateException("更新商品数量时出现未知的异常");
            }
        }
    }

    /**
     * 获取畅销商品
     * @return
     */
    @Override
    public List<Product> getProduct() {
        List<Product> exists = productMapper.selectHotProduct();
        if(exists.isEmpty()){
            throw new ProductNotFoundException("商品找不到异常！");
        }else {
            exists.forEach(product -> {
                product.setModifiedTime(null);
                product.setModifiedUser(null);
                product.setCreatedTime(null);
                product.setCreatedUser(null);
            });
        }
        return exists;
    }

    /**
     * 获取新到商品
     * @return
     */
    @Override
    public List<Product> getNewProduct() {
        List<Product> exists = productMapper.selectNewProduct();
        if(exists.isEmpty()){
            throw new ProductNotFoundException("商品找不到异常！");
        }else {
            exists.forEach(product -> {
                product.setModifiedTime(null);
                product.setModifiedUser(null);
                product.setCreatedTime(null);
                product.setCreatedUser(null);
            });
        }
        return exists;
    }

    /**
     * 模糊查询
     * @param name 商品名
     * @return
     */
    @Override
    public List<Product> getProductBySearch(String name) {
        List<Product> exists = productMapper.selectProductBySearch(name);
        if(exists.size() == 0){
            throw new ProductNotFoundException("商品找不到异常！");
        }else {
            exists.forEach(product -> {
                product.setModifiedTime(null);
                product.setModifiedUser(null);
                product.setCreatedTime(null);
                product.setCreatedUser(null);
            });
        }
        return exists;
    }


}
